#!/user/bin/env python
# -*- coding:utf-8 -*-
#/home/zc/workspace/tinyfeet/trunk/src/controllers/utils/tfuser.py

'''
返回值字段    字段类型    字段说明
id    int64    用户UID
screen_name    string    用户昵称
province    int    用户所在地区ID
city    int    用户所在城市ID
location    string    用户所在地
description    string    用户描述
url    string    用户博客地址
profile_image_url    string    用户头像地址
domain    string    用户的个性化域名
gender    string    性别，m：男、f：女、n：未知
avatar_large    string    用户大头像地址
online_status    int    用户的在线状态，0：不在线、1：在线

Created on 2011-9-18

@author: zc
'''

from dbhandle.mysqldb import mysqldb
from datetime import datetime
from config.sqlmap import SQL_ADD_USER, SQL_UPDATE_USER, SQL_GET_USER, SQL_GET_CNTUSER
import logger
logger = logger.logger()


dbo = mysqldb()
s_date = datetime.now().strftime( "%Y%m%d%H%M%S" )

def updateUser( user, lastip = None ):
    res = -1
    try:
        user_cnt = dbo.query( SQL_GET_CNTUSER % user.id )
        if user_cnt and user_cnt[0].cnt == 0 :
            res = addUser( user )
        else:
            sql = SQL_UPDATE_USER % ( user.id, user.screen_name, user.province, user.city,
                                     user.location, user.description, user.profile_image_url,
                                     user.avatar_large, user.url, user.domain, user.gender,
                                     s_date, lastip, user.id )
            res = dbo.execsql( sql )
    except Exception, data:
        logger.error( data )
        print 'updateUser Error-->>', data
    
    return res


def addUser( user, lastip = None ):
    res = -1
    try:
        sql = SQL_ADD_USER % ( user.id, user.screen_name, user.province, user.city,
                                 user.location, user.description, user.profile_image_url,
                                 user.avatar_large, user.url, user.domain, user.gender,
                                 s_date, s_date, lastip )
        res = dbo.execsql( sql )
    except Exception, data:
        logger.error( data )
        print 'addUser Error-->>>', data
        
    return res


def getUser( uid ):
    """ return a dict """
    user = None
    sql = SQL_GET_USER % ( uid )
    res = dbo.query( sql )
    if res :
        user = res[0]
    return user


    
if __name__ == '__main__':
    r = addUser( "来晚了名字被抢了", "weibo", "nothing", "http:/asdf/asdf" )
    print r
